﻿@model Apps.Models.WMS.WMS_AIModel
@using Apps.Web.Core;
@using Apps.Common;
@using Apps.Models.Spl;
@using Apps.Web;
@using Apps.Locale;
@using Apps.Models.Sys;
@{
    ViewBag.Title = "创建到货";
    Layout = "~/Views/Shared/_Index_LayoutEdit.cshtml";
    List<permModel> perm = null;
}

<script type="text/javascript">
    $(function ()
    {
        //自动加载PO
        $("#btnLoadPO").click(function ()
        {
            if ($("#isAutoLoad")[0].checked)
            {
                $('#pAutoLoad').panel('open')
                $('#pManualLoad').panel('close')
                var poNo = $("#PONo").val();
                $("#List").datagrid("load", { poNo: poNo });
            }
            else
            {
                $('#pAutoLoad').panel('close')
                $('#pManualLoad').panel('open')
                $("#mList").datagrid("load");
            }
        });

        $("#btnSave").click(function ()
        {
            if (!$("form").valid()) {
                $.messageBox5s('提示', $("#ErrMesList").html());
                return;
            }
            if ($("form").valid())
            {
                if ($("#isAutoLoad")[0].checked)
                {
                    endEditing();
                    var $dg = $('#List');
                    var rows = $dg.datagrid('getChecked');  //获取CheckBox为选中的行
                }
                else
                {
                    endEditing_mList();
                    var $dg = $('#mList');
                    var rows = $dg.datagrid('getRows');  //获取所有的行
                    for (var i = 0; i < rows.length; i++)
                    {
                        if (!rows[i]['Id'])
                        {
                            alert("存在无效的物料，请确认！");
                            return;
                        }
                    }
                    //将所选中的行转换成json字符串，并保存到inserted隐藏输入框中。
                    //$("#inserted").val(JSON.stringify(rows, function (key, value)
                    //{
                    //    if(key == 'oper'){
                    //        return undefined;
                    //    }
                    //    return value;
                    //}));
                }

                //将所选中的行转换成json字符串，并保存到inserted隐藏输入框中。
                $("#inserted").val(JSON.stringify(rows));

                if (rows.length) {
                    showLoading();
                    $.ajax({
                        url: "@Url.Action("Create")",
                        type: "Post",
                        data: $("form").serialize(),
                        dataType: "json",
                        success: function (data) {
                            if (data.type == 1) {
                                hideLoading();
                                window.parent.frameReturnByMes(data.message);
                                window.parent.frameReturnByReload(true);
                                window.parent.frameReturnByMes("保存成功！");
                                //保存后不关闭当前对话框，但把控件数据清空
                                window.parent.frameReturnByRefresh();
                                //clear();

                            } else {
                                window.parent.frameReturnByMes(data.message);
                                hideLoading();

                            }
                        }
                    });
                } else {
                    $.messageBox5s('提示', "没有数据需要保存！");
                }
            }
        });
        $("#btnReturn").click(function () {
            window.parent.frameReturnByClose();
        });

        $(window).resize(function () {
            $('#List').datagrid('resize', {

            }).datagrid('resize', {
                width: SetGridWidthSub(30),
                height: SetGridHeightSub(50)
            });
        });

    });
    $(function ()
    {
        $('#isAutoLoad').switchbutton({
            checked: true,
            onText: '自动加载',
            offText: '手动加载',
            width: 100,
            onChange: function (checked)
            {
                if (checked)
                {
                    $('#pAutoLoad').panel('open')
                    $('#pManualLoad').panel('close')
                }
                else
                {
                    $('#pAutoLoad').panel('close')
                    $('#pManualLoad').panel('open')
                }
            },
        });

        //自动加载的datagrid
        $('#List').datagrid({
            url: '@Url.Action("GetPODetailsList")?poNo=""',
            width: SetGridWidthSub(10),
            methord: 'post',
            height: SetGridHeightSub(202),
            fitColumns: true,
            idField: 'Id',
            pageSize: 15,
            pageList: [15, 20, 30, 40, 50],
            pagination: true,
            striped: true, //奇偶行是否区分
            singleSelect: true,//单选模式
            selectOnCheck: false,
            checkOnSelect: false,
            onClickCell: onClickCell,
            onEndEdit: onEndEdit,
            onBeginEdit: onBeginEdit,
            remotesort: false,
            showFooter: false,
            rowStyler: function (index, row)
            {
                if (Number(row.SumAIQty) >= Number(row.QTY))
                {
                    return 'background: #D24D57; color: #fff';
                }
                if (Number(row.SumAIQty) < Number(row.QTY) && Number(row.SumAIQty) > 0) {
                    return 'background: #26A65B; color: #fff';
                }
            },
            onLoadSuccess: function (data) {
                //append();
                if (data.rows.length > 0)
                {
                    $("input[type='checkbox']")[0].disabled = true;
                    //循环判断操作为新增的不能选择
                    for (var i = 0; i < data.rows.length; i++)
                    {
                        $("input[type='checkbox']")[i + 1].disabled = true;
                    }

                    //将POId赋值给Input输入框
                    //$("#Id").val(data.rows[0].Id);
                }
                else
                {
                    if ($("#PONo").val() != '')
                        alert('没有需要处理的数据！');
                }
            },
            columns: [[
                {
                    field: 'ck', checkbox: true,
                },
                { field: 'Id', title: 'Id', width: 80, hidden: true },
                { field: 'PO', title: '采购订单', width: 80,  hidden: true },
                { field: 'PartId', title: '物料ID', width: 80,  hidden: true },
                { field: 'PartCode', title: '物料编码', width: 80, sortable: true },
                { field: 'CurrentQty', title: '本次到货数量', width: 80, editor: { type: 'numberbox', options: { required: true, min: 0 } }, align: 'right' },
                { field: 'BoxNum', title: '到货箱数', width: 80, editor: { type: 'numberbox' }, align: 'right' },
                {
                    field: 'ArrivalDate', title: '到货日期', width: 80, sortable: true,
                    //formatter: function (value, row, index)
                    //{
                    //    return formatterDateNoHour(value);
                    //},
                    editor: { type: 'datetimebox' }
                },
                {
                    field: 'PlanDate', title: '计划到货日期', width: 80, sortable: true,
                    formatter: function (value, row, index)
                    {
                        return formatterDateNoHour(value);
                    }
                },
                { field: 'QTY', title: '订单数量', width: 80, sortable: true, align: 'right' },
                { field: 'SumAIQty', title: '已到货数量', width: 80, sortable: true, align: 'right'/*, formatter: function (value) { return CheckFormatter(value) }*/},

            ]]
        });

        //手动加载的datagrid
        $('#mList').datagrid({
            url: '@Url.Action("GetPODetailsList")?poNo=""',
            methord: 'post',
            width: SetGridWidthSub(10),
            height: SetGridHeightSub(202),
            fitColumns: true,
            idField: 'Id',
            pagination: false,
            striped: true, //奇偶行是否区分
            singleSelect: true,//单选模式
            selectOnCheck: false,
            checkOnSelect: false,
            onClickCell: onClickCell_mList,
            onEndEdit: onEndEdit_mList,
            onBeginEdit: onBeginEdit_mList,
            remotesort: false,
            showFooter: false,
            rowStyler: function (index, row)
            {
                if (Number(row.SumAIQty) >= Number(row.QTY))
                {
                    return 'background: #D24D57; color: #fff';
                }
                if (Number(row.SumAIQty) < Number(row.QTY) && Number(row.SumAIQty) > 0) {
                    return 'background: #26A65B; color: #fff';
                }
            },
            onLoadSuccess: function (data)
            {
                append();
            },
            columns: [[
                { field: 'Id', title: 'Id', width: 80, hidden: true },
                { field: 'PO', title: '采购订单', width: 80, hidden: true },
                {
                    field: 'oper', title: '操作', width: 50, formatter: function (value)
                    {
                        if (value == null)
                            return "<a class='fa fa-plus color-green' href='javascript:append()'><a>&nbsp;&nbsp;&nbsp;<a class='fa fa-minus color-red' href='javascript:removeit()'><a>";
                        else
                            return value;
                    }
                },
                { field: 'PartId', title: '物料ID', width: 80, hidden: true },
                {
                    field: 'PartCode', title: '物料编码', width: 100,
                    editor: { type: 'textbox', options: { required: true } }
                },
                { field: 'CurrentQty', title: '本次到货数量', width: 80, editor: { type: 'numberbox', options: { required: true, min: 0 } }, align: 'right' },
                { field: 'BoxNum', title: '到货箱数', width: 80, editor: { type: 'numberbox' }, align: 'right' },
                {
                    field: 'ArrivalDate', title: '到货日期', width: 80, sortable: true,
                    //formatter: function (value, row, index)
                    //{
                    //    return formatterDateNoHour(value);
                    //},
                    editor: { type: 'datetimebox' }
                },
                {
                    field: 'PlanDate', title: '计划到货日期', width: 80, sortable: true,
                    formatter: function (value, row, index)
                    {
                        return formatterDateNoHour(value);
                    }
                },
                { field: 'QTY', title: '订单数量', width: 80, sortable: true, align: 'right' },
                { field: 'SumAIQty', title: '已到货数量', width: 80, sortable: true, align: 'right'/*, formatter: function (value) { return CheckFormatter(value) }*/},

            ]]
        });
    });
    //输入到货数量（<>0）后自动选择当前行
    function setKeyUpValue(sender, datagrid)
    {
        var row = datagrid.datagrid('getSelected');
        var rowIndex = datagrid.datagrid('getRowIndex', row);//获取行号
        var thisTarget = datagrid.datagrid('getEditor', { 'index': rowIndex, 'field': 'CurrentQty' }).target;
        var thisTarget_Input = $(thisTarget).data('numberbox') ? $(thisTarget).numberbox('textbox') : $(thisTarget);

        //如果当前供应商不允许超量接收，则判断当前到货数量+已到货数量是否>订单数量，如大于则为0
        if ($("#MoreAccept").val() == "不允许")
        {
            if (Number(thisTarget_Input.val()) + Number(row.SumAIQty) > Number(row.QTY))
            {
                thisTarget_Input.val(0);
                alert("当前供应商不允许超量接收！");
            }
        }

        if (thisTarget_Input.val() == "")
            thisTarget_Input.val(0);

        if ($("#isAutoLoad")[0].checked)
        {
            if (thisTarget_Input.val() != 0)
            {
                datagrid.datagrid('checkRow', rowIndex);
            }
            else
            {
                datagrid.datagrid('uncheckRow', rowIndex);
            }
            $("input[type='checkbox']")[rowIndex + 1].disabled = true;
        }

        //如果到货日期为空，则给到货日期赋值当前日期
        var dateTarget = datagrid.datagrid('getEditor', { 'index': rowIndex, 'field': 'ArrivalDate' }).target;
        if (dateTarget.datebox('getValue') == "")
        {
            //dateTarget.datebox('setValue', formatterDate(new Date()));
            dateTarget.datebox('setValue', new Date());
        }
    }

    //得到当前日期
    formatterDate = function (date)
    {
        var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
        var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0"
            + (date.getMonth() + 1);
        return date.getFullYear() + '-' + month + '-' + day;
    };

    //编辑的行
    var editIndex = undefined;
    function endEditing() {
        if (editIndex == undefined) { return true }
        $('#List').datagrid('endEdit', editIndex);

        //执行上面的代码会使得checkbox可编辑，需要手工再次设置disabled = true
        $("input[type='checkbox']")[editIndex + 1].disabled = true;

        editIndex = undefined;
        return true;
    }
    function onClickCell(index, field) {
        if (editIndex != index) {
            if (endEditing()) {
                $('#List').datagrid('selectRow', index)
                        .datagrid('beginEdit', index);

                //给numberbox增加keyup事件，以便自动计算合格数量和不合格数量
                var eds = $('#List').datagrid('getEditors', index);
                for (var i = 0; i < eds.length; i++)
                {
                    var ed = eds[i];
                    //给字段CurrentQty增加setKeyUpValue事件
                    if (ed.field == 'CurrentQty')
                    {
                        var target = $(ed.target).data('numberbox') ? $(ed.target).numberbox('textbox') : $(ed.target);
                        target.attr("field", ed.field)
                        target.bind("keyup", function ()
                        {
                            return setKeyUpValue($(this), $('#List'));
                        });
                    }
                }

                var ed = $('#List').datagrid('getEditor', { index: index, field: field });
                if (ed) {
                    ($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
                    ($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).select();
                }
                editIndex = index;
            } else {
                setTimeout(function () {
                    $('#List').datagrid('selectRow', editIndex);
                }, 0);
            }
        }
    }
    function onEndEdit(index, row) {

    }
    //进入编辑前设置
    function onBeginEdit(index, row) {

    }

    //#region 手动加载DataGrid的事件
    //编辑的行
    var editIndex_mList = undefined;
    function endEditing_mList()
    {
        if (editIndex_mList == undefined) { return true }
        $('#mList').datagrid('endEdit', editIndex_mList);

        editIndex_mList = undefined;
        return true;
    }

    //添加空行—手动加载
    function append()
    {
        var index = $('#mList').datagrid('getRowIndex', $('#mList').datagrid('getSelected'));
        if (index == -1)
            index = 0;
        $("#mList").datagrid("insertRow", {
            index: index + 1,
            row: {
                oper: "<a class='fa fa-plus color-green' href='javascript:append()'><a>&nbsp;&nbsp;&nbsp;<a class='fa fa-minus color-red' href='javascript:removeit()'><a>",
                PO: $("#PONo").val(),
                CurrentQty: 0,
            }
        });
    }

    //删除一行—手动加载
    function removeit()
    {
        if (editIndex_mList == undefined) { return }
        $('#mList').datagrid('selectRow', editIndex_mList);

        $('#mList').datagrid('cancelEdit', editIndex_mList)
            .datagrid('deleteRow', editIndex_mList);
        editIndex_mList = undefined;
    }

    function onEndEdit_mList(index, row)
    {

    }
    //进入编辑前设置
    function onBeginEdit_mList(index, row)
    {

    }

    function onClickCell_mList(index, field)
    {
        if (editIndex_mList != index)
        {
            if (endEditing_mList())
            {
                $('#mList').datagrid('selectRow', index)
                    .datagrid('beginEdit', index);

                //给numberbox增加keyup事件，以便自动计算合格数量和不合格数量
                var eds = $('#mList').datagrid('getEditors', index);
                for (var i = 0; i < eds.length; i++)
                {
                    var ed = eds[i];
                    //给字段PartCode增加setKeyUpValue事件
                    if (ed.field == 'PartCode')
                    {
                        var target = $(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target);
                        target.bind("keyup", function (e)
                        {
                            return setKeyUpValueForPartCode(e, $(this), $('#mList'));
                        });
                    }

                    //给字段CurrentQty增加setKeyUpValue事件
                    if (ed.field == 'CurrentQty')
                    {
                        var target = $(ed.target).data('numberbox') ? $(ed.target).numberbox('textbox') : $(ed.target);
                        target.attr("field", ed.field)
                        target.bind("keyup", function ()
                        {
                            return setKeyUpValue($(this), $('#mList'));
                        });
                    }
                }

                var ed = $('#mList').datagrid('getEditor', { index: index, field: field });
                if (ed)
                {
                    ($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
                    ($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).select();
                }
                editIndex_mList = index;
            } else
            {
                setTimeout(function ()
                {
                    $('#mList').datagrid('selectRow', editIndex);
                }, 0);
            }
        }
    }

    function setKeyUpValueForPartCode(e, sender, datagrid)
    {
        var code = e.keyCode || e.which;
        if (code == 13)
        {
            var row = datagrid.datagrid('getSelected');
            var rowIndex = datagrid.datagrid('getRowIndex', row);//获取行号
            var thisTarget = datagrid.datagrid('getEditor', { 'index': rowIndex, 'field': 'PartCode' }).target;
            var thisTarget_Input = $(thisTarget).data('textbox') ? $(thisTarget).textbox('textbox') : $(thisTarget);

            //向后台申请当前PO+物料的行信息
            $.ajax({
                url: "@Url.Action("GetPODetailsListByPartCode")",
                type: "Post",
                data: { poNo: $("#PONo").val(), partCode: thisTarget_Input.val()},
                dataType: "json",
                success: function (data) {
                    if (data.type == 1)
                    {
                        //alert(JSON.parse(data.value));
                        getRow = JSON.parse(data.value);
                        datagrid.datagrid('updateRow', {
                            index: rowIndex,
                            row: getRow,
                        });
                        endEditing_mList();
                        onClickCell_mList(rowIndex, "CurrentQty");
                    } else
                    {
                        alert(data.message);
                        row.Id = "";
                        row.PO = "";
                        row.PartId = "";
                        row.PartCode = "";
                        row.CurrentQty = 0;
                        row.BoxNum = 0;
                        row.ArrivalDate = "";
                        row.PlanDate = "";
                        row.QTY = "";
                        row.SumAIQty = "";

                        datagrid.datagrid('updateRow', {
                            index: rowIndex,
                            row: row,
                        });
                        endEditing_mList();
                    }
                }
            });
        }

    }
    //#endregion

    //关闭模式窗口
    function frameReturnByClose() {
        $("#modalwindow").window('close');
    }


    //function removeit() {
    //    if (editIndex == undefined) { return }
    //    $('#List').datagrid('selectRow', editIndex);

    //    $('#List').datagrid('cancelEdit', editIndex)
    //       .datagrid('deleteRow', editIndex);
    //    editIndex = undefined;
    //}

    function reject() {
        $('#List').datagrid('rejectChanges');
        editIndex = undefined;
    }
    function getChanges() {
        var rows = $('#List').datagrid('getChanges');
        alert(rows.length + ' rows are changed!');
    }

    function contains(arr, obj) {
        var i = arr.length;
        while (i--) {
            if (arr[i] === obj) {
                return true;
            }
        }
        return false;
    }

    //ifram 返回
    function frameReturnByClose() {
        $("#modalwindow").window('close');
    }

    function frameReturnByMes(mes) {
        $.messageBox5s('提示', mes);
    }

    //清空控件的数据（暂时没用）。
    function clear()
    {
        $('#PONo').val("");
        $('#List').datagrid("Load");

    }
</script>

<div id="modalwindow" class="easyui-window" style="width:800px; height:400px;" data-options="modal:true,closed:true,minimizable:false,shadow:false"></div>
<div class="mvctool bgb">
    @Html.ToolButton("btnSave", "fa fa-save", Resource.Save, ref perm, "Save", true)
    @Html.ToolButton("btnReturn", "fa fa-reply", Resource.Reply, false)
</div>
@using (Html.BeginForm())
{
    <div id="ErrMesList">
        <div id="ErrMesListContent">
            @Html.ValidationSummary(false)
        </div>
    </div>
    @*@Html.HiddenFor(model => model.Id)*@
    <input id="inserted" type="hidden" name="inserted" value="" />
    <table class="formtable">
        <tbody>
            <tr>
                <th>
                    @*@Html.LabelFor(model => model.POId)*@
                    采购订单：
                </th>
                <td>
                    <div style="height:0;width:0;overflow:hidden">
                        @*@Html.TextBoxFor(model => model.POId)*@
                    </div>
                    <input id="PONo" name="PONo" type="text" style="width: 200px; display: inline;">
                    <a class="fa fa-plus-square color-gray fa-lg" id="selExc" href="javascript:showSelPO()"></a>
                    <script type="text/javascript">
                        function showSelPO()
                        {
                            $("#modalwindow").html("<iframe width='100%' height='100%' scrolling='no' frameborder='0'' src='/WMS/PO/POLookUp'></iframe>");
                            $("#modalwindow").window({ title: '选择采购订单', width: 720, height: 488, iconCls: 'fa fa-plus' }).window('open');
                        }

                        function SetSelResult(result, resultName, moreAccept)
                        {
                            //$("#POId").val(result);
                            $("#PONo").val(resultName);
                            $("#MoreAccept").val(moreAccept);
                        }

                    </script>
                    @Html.ToolButton("btnLoadPO", "fa fa-reply", Resource.Load, false)
                    <input id="isAutoLoad" style="width:200px;height:30px">
                </td>
                <th>
                    @Html.LabelFor(model => model.ArrivalBillNum) （自动生成）：
                </th>
                <td>
                    <input id="arrivalBillNum" type="hidden" name="arrivalBillNum" value="@ViewBag.ArrivalBillNum" />
                    @Html.TextBox("arrivalBillNum", (string)ViewBag.ArrivalBillNum, new { @disabled = "disabled" })
                </td>
                <th>
                    是否允许超量接收：
                </th>
                <td>
                    <input id="MoreAccept" name="MoreAccept" type="text" style="width: 200px; display: inline;">
                </td>
            </tr>

        </tbody>
    </table>
}
<div id="pAutoLoad" class="easyui-panel" title="自动加载">
    <table>
        <tr>
            <td colspan="7" style="padding:4px;">
                <table id="List"></table>
            </td>
        </tr>

    </table>
</div>
<div id="pManualLoad" class="easyui-panel" title="手动加载" data-options="closed:true">
    <table>
        <tr>
            <td colspan="7" style="padding:4px;">
                <table id="mList"></table>
            </td>
        </tr>

    </table>
</div>
